package com.hotel.suqiang.dingdonegurad.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hotel.suqiang.dingdonegurad.R;

/**
 * Created by suqiang on 17/6/1.
 */

public class SettingView extends RelativeLayout {


    private View view;
    private ImageView iv_switch;
    private TextView tv_title;

    private boolean isToggle;

    public SettingView(Context context) {
        super(context);

        initView();
    }

    public SettingView(Context context, AttributeSet attrs) {
        super(context, attrs);

        initView();

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingView);

        String title = typedArray.getString(R.styleable.SettingView_title);

        int bkg = typedArray.getInt(R.styleable.SettingView_setBackground, 0);

        boolean isShow = typedArray.getBoolean(R.styleable.SettingView_isToggle, false);

        typedArray.recycle();

        tv_title.setText(title);

        if (isShow){
            iv_switch.setVisibility(VISIBLE);
        }else {
            iv_switch.setVisibility(GONE);
        }

        int bkgId = R.drawable.setting_item_first_selector;
        switch (bkg){
            case 0:{
                bkgId = R.drawable.setting_item_first_selector;
               break;

            }
            case 1:{
                bkgId = R.drawable.setting_item_middle_selector;
                break;
            }
            case 2:{
                bkgId = R.drawable.setting_item_last_selector;
                break;
            }
            default:{
                bkgId = R.drawable.setting_item_first_selector;
            }
        }

        view.setBackgroundResource(bkgId);

    }

    public SettingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView();
    }

    private void initView() {
        view =  View.inflate(getContext(), R.layout.setting_view_layout, this);

        iv_switch = (ImageView) view.findViewById(R.id.iv_setting_switch);

        tv_title = (TextView) view.findViewById(R.id.tv_setting_title);
    }

    public void setToggle(boolean toggle){
        this.isToggle = toggle;
        if (toggle) {
            iv_switch.setImageResource(R.drawable.on);
        }else{
            iv_switch.setImageResource(R.drawable.off);
        }
    }

    public boolean getToggle(){
        return isToggle;
    }

    public void toggle(){
        setToggle(!isToggle);
    }


}
